<script>
function deletecustomer(id) {
	if (confirm('You are about to delete this customer from the system? This will also delete all orders made by this customer? Are you sure you want to do this?')) {
		/*window.location = '<?=base_url()?>admin/store/deletecustomer/' + id;*/
	}
}
function export_csv() {
	if (confirm('This will export the customers list to a csv file. Do you want to continue?')) {
		//var type=document.customerform.type.value;
		window.location = '<?=base_url()?>admin/store/exportcustomer';
	}
}
function deletesubscribe(id) {
	if (confirm('You are about to delete this subscribe from the system? Are you sure you want to do this?')) {
		window.location = '<?=base_url()?>admin/store/deletesubscribe/' + id;
	}
}
</script>
<? //echo $this->session->userdata('store_number');?>

    	<div class="left">
        	<h1>Store Management</h1>
            <div class="bar">

            	<div class="text">Manage Customers</div>
            	<div class="cr"></div>
            </div>
            <div class="box bgw">
               <a href="<?=base_url()?>admin/store/customer/add"><input type="button" class="button rounded" value="Add New Customer" /></a>&nbsp;&nbsp;<a href="<?=base_url()?>admin/store/customer/import"><input type="button" class="button rounded" value="Import Customer CSV" /></a>
               <?php if($this->session->flashdata('customer_import'))
			   {
				    echo '<div class="green"> &nbsp; Customer has been added successfully!</div>';
			   }
			   ?>
            </div>
             <hr />
            <div class="box">
            	<h3>Search Customer</h3>
            	<form name="customerform" method="post" action="<?=base_url()?>admin/store/customer/search">
                <dl class="three"><dt>Customer name</dt><dd><input type="text" class="textfield rounded" name="name" value="<? if ($this->session->userdata('name')){echo $this->session->userdata('name');}?>" /></dd></dl>
                <!--
                <dl class="three"><dt>Customer type</dt><dd>
                	<select name="type">
                		<option value="4">Franchise</option>
                        <option value="5">Subscribers</option>
                	</select>
                </dd>
                -->
                <dl class="three"><dt>Store Number</dt><dd><input type="text" class="textfield rounded" name="store_number" value="<? if ($this->session->userdata('store_number')){echo $this->session->userdata('store_number');}?>" /></dd></dl>
                <dd>&nbsp;&nbsp;
                <input type="submit" class="button rounded" value="Search" /></dd>
                </dl>
                <dl></dl>
                </form>
            </div>
            <hr />
            <div class="box bgw">
            	<h3>Customers List &nbsp; <input type="button" class="button rounded" value="Export to CSV" onclick="export_csv()" /></h3>
                <p class="desc">Use the search function to search through all customers in the system.</p><br />
                <div class="row-title">
                  <?php
						if($this->session->userdata('type')!=5)
						{
					?>
                	<div class="cust-uname" style="border:0 none">Store No</div>
                	<div class="cust-fname" style="border-left: 1px dotted #63A2D4;">Store Name</div>
                    <div class="cust-email">Trading Name</div>
                    
                    <div class="cat-func">Edit</div>
                    
                    <div class="cat-func">Status</div>
                   
                    <div class="cat-func2">Orders</div>
                    <?php
						}
						else
						{
						?>
                        <div class="cust-fname">First Name</div>
                	<div class="cust-fname" style="border-left: 1px dotted #63A2D4;">Family Name</div>
                    <div class="cust-fname" style="border-left: 1px dotted #63A2D4;">Email</div>
                    <div class="cust-uname">Phone</div>
                     <div class="cat-func">Delete</div>
                       <?
						}
						?>
                </div>
                <div id="subcat">
                  <?php 
				  if($this->session->userdata('type')==5)
				  {	
				    foreach($subscribers as $s) 
					{ ?>                	
                    <div class="row-item">
                        <div class="cust-fname"><?=$s['firstname']?></div>    
                        <div class="cust-fname" style="border-left: 1px dotted #63A2D4;"><?=$s['familyname']?></div>
                         <div class="cust-fname" style="border-left: 1px dotted #63A2D4;"><?=$s['email']?></div>  
                          <div class="cust-uname"><?=to_short($s['mobile'],20)?></div>                     
                        <div class="cat-func"><a href="javascript:deletesubscriber(<?=$s['id']?>)"><img src="<?=base_url()?>img/backend/icon-delete.png" /></a></div>                      
						
                    </div>
                    <?php 
						
					}
				  }
				  else
				  {
                     foreach($users as $user) { 
                	$customer = $this->Customer_model->identify($user['customer_id']); ?>
                    <div class="row-item">
                    	<div class="cust-uname" style="border:0 none"><?= $customer['storeno']?>
						
						</div>
                    	<div class="cust-fname" style="border-left: 1px dotted #63A2D4;"><?= to_short($customer['storename'],14)?></div>
                        <div class="cust-email"><?=to_short($customer['tradingname'],22)?></div>
                        <?php if($user['level'] == 1) { ?>
                        <div class="cat-func"><a href="javascript:deletecustomer(<?=$customer['id']?>)"><img src="<?=base_url()?>img/backend/icon-delete.png" /></a></div>
                        <?php } ?>
                        <div class="cat-func"><a href="<?=base_url()?>admin/store/customer/edit/<?=$customer['id']?>"><img src="<?=base_url()?>img/backend/icon-view.png" /></a></div>
                        <?php if($user['level'] == 4) { 
                        	if($user['activated']) { ?>
                        <div class="cat-func"><img src="<?=base_url()?>img/backend/icon-actived.png" title="Approved" /></div>
                        <?php } else { ?>
                        <div class="cat-func"><img src="<?=base_url()?>img/backend/icon-inactived.png" title="Pending" /></div>
                        <?php } } ?>
                        
                        <div class="cat-func2"><?=$this->Customer_model->total_orders($customer['id'],'')?> (<span class="green"><?=$this->Customer_model->total_orders($customer['id'],'successful')?></span>/<span class="error"><?=$this->Customer_model->total_orders($customer['id'],'failed')?></span>)</div>
                    </div>
                    <?php 
					 }
				} ?>
                </div>
            </div>
        </div>
        
